package cn.signalpha.modules.system.controller;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.constant.CommonConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.cache.utils.CacheUtil;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.constant.SystemConstant;
import cn.signalpha.core.tool.utils.StringPool;
import cn.signalpha.modules.system.entity.Dict;
import cn.signalpha.modules.system.service.IDictService;
import cn.signalpha.modules.system.vo.DictVO;
import cn.signalpha.modules.system.wrapper.DictWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import static cn.signalpha.core.cache.constant.CacheConstant.DICT_CACHE;

/**
 * 控制器
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SYSTEM_NAME + "/admin/dict")
@Api(value = "字典", tags = "字典")
public class DictController extends BaseController {

	private final IDictService dictService;

	@GetMapping("/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "code", value = "字典编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "dictValue", value = "字典名称", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "列表", notes = "传入dict")
	public R<List<DictVO>> list(@ApiIgnore @RequestParam Map<String, Object> dict) {
		List<Dict> list = dictService.list(Condition.getQueryWrapper(dict, Dict.class).lambda().orderByAsc(Dict::getSort));
		return R.data(DictWrapper.build().listNodeVO(list));
	}

	@GetMapping("/tree")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "树形结构", notes = "树形结构")
	public R<List<DictVO>> tree() {
		return R.data(dictService.tree());
	}

	@GetMapping("/parent-list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "code", value = "字典编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "dictValue", value = "字典名称", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "父列表", notes = "传入dict")
	public R<IPage<DictVO>> parentList(@ApiIgnore @RequestParam Map<String, Object> dict, Query query) {
		return R.data(dictService.parentList(dict, query));
	}

	@GetMapping("/parent-tree")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "父树形结构", notes = "树形结构")
	public R<List<DictVO>> parentTree() {
		return R.data(dictService.parentTree());
	}

	@GetMapping("/child-list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "code", value = "字典编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "dictValue", value = "字典名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "parentId", value = "字典名称", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "子列表", notes = "传入dict")
	public R<List<DictVO>> childList(@ApiIgnore @RequestParam Map<String, Object> dict, @RequestParam(required = false, defaultValue = "-1") Long parentId) {
		return R.data(dictService.childList(dict, parentId));
	}

	@GetMapping("/select")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "字典键值列表", notes = "字典键值列表")
	public R<List<Dict>> select() {
		List<Dict> list = dictService.list(Wrappers.<Dict>query().lambda().eq(Dict::getParentId, CommonConstant.TOP_PARENT_ID));
		list.forEach(dict -> dict.setDictValue(dict.getCode() + StringPool.COLON + StringPool.SPACE + dict.getDictValue()));
		return R.data(list);
	}

	@GetMapping("/select-all")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "字典全列表", notes = "字典全列表")
	public R<List<Dict>> selectAll() {
		List<Dict> list = dictService.list(Wrappers.<Dict>query().lambda().eq(Dict::getIsDeleted, SystemConstant.DB_NOT_DELETED));
		return R.data(list);
	}

	@GetMapping("/detail")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "详情", notes = "传入dict")
	public R<DictVO> detail(Dict dict) {
		Dict detail = dictService.getOne(Condition.getQueryWrapper(dict));
		return R.data(DictWrapper.build().entityVO(detail));
	}

	@GetMapping("/dictionary")
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "获取字典", notes = "获取字典")
	public R<List<Dict>> dictionary(String code) {
		List<Dict> tree = dictService.getList(code);
		return R.data(tree);
	}

	@GetMapping("/dictionary-tree")
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "获取字典树", notes = "获取字典树")
	public R<List<DictVO>> dictionaryTree(String code) {
		List<Dict> tree = dictService.getList(code);
		return R.data(DictWrapper.build().listNodeVO(tree));
	}

	@PostMapping("/submit")
	@ApiOperationSupport(order = 20)
	@ApiOperation(value = "提交", notes = "传入dict")
	public R submit(@Valid @RequestBody Dict dict) {
		CacheUtil.clear(DICT_CACHE, Boolean.FALSE);
		return R.status(dictService.submit(dict));
	}

	@PostMapping("/remove")
	@ApiOperationSupport(order = 50)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		CacheUtil.clear(DICT_CACHE, Boolean.FALSE);
		return R.status(dictService.removeDict(ids));
	}

}
